package com.jyuxuan.recruitment.controller;

import com.jyuxuan.recruitment.config.shiro.RoleToken;
import com.jyuxuan.recruitment.constant.CodeType;
import com.jyuxuan.recruitment.constant.RoleType;
import com.jyuxuan.recruitment.pojo.Admin;
import com.jyuxuan.recruitment.pojo.EnterpriseLogin;
import com.jyuxuan.recruitment.service.impl.EnterpriseServiceImpl;
import com.jyuxuan.recruitment.utils.DataMap;
import com.jyuxuan.recruitment.utils.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
@RequestMapping("/login")
public class LoginController {

    @Autowired
    EnterpriseServiceImpl enterpriseService;

    /**
     * 企业登录
     *
     * @param enterpriseLogin
     * @return
     */
    @PostMapping("enterpriseLogin")
    @ResponseBody
    public String enterpriseLogin(EnterpriseLogin enterpriseLogin) {
        Subject subject = SecurityUtils.getSubject();
        RoleToken token = new RoleToken(enterpriseLogin.getNumber(), enterpriseLogin.getPassword(), RoleType.ENTERPRISE);
        DataMap data = null;
        try {
            subject.login(token);
            data = DataMap.success(CodeType.SUCCESS_LOGIN);
        } catch (UnknownAccountException e) {
            data = DataMap.fail(CodeType.NOT_FOUND_NUMBER);
        } catch (IncorrectCredentialsException e) {
            data = DataMap.fail(CodeType.WRONG_PASSWORD);
        }
        return JsonResult.build(data).toJSON();
    }

    /**
     * 管理员登录
     *
     * @param admin
     * @return
     */
    @PostMapping("adminLogin")
    @ResponseBody
    public String adminLogin(Admin admin) {
        Subject subject = SecurityUtils.getSubject();
        RoleToken token = new RoleToken(admin.getNumber(), admin.getPassword(), RoleType.ADMIN);
        DataMap data = null;
        try {
            subject.login(token);
            data = DataMap.success(CodeType.SUCCESS_LOGIN);
        } catch (UnknownAccountException e) {
            data = DataMap.fail(CodeType.NOT_FOUND_NUMBER);
        } catch (IncorrectCredentialsException e) {
            data = DataMap.fail(CodeType.WRONG_PASSWORD);
        }
        return JsonResult.build(data).toJSON();
    }

    /**
     * 企业注册
     *
     * @param enterpriseLogin
     * @return
     */
    @PostMapping("enterRegister")
    @ResponseBody
    public String enterRegister(EnterpriseLogin enterpriseLogin) {
        DataMap data = enterpriseService.enterpriseRegister(enterpriseLogin);
        return JsonResult.build(data).toJSON();
    }

}
